// 主程序入口
document.addEventListener('DOMContentLoaded', function() {
    // 初始化游戏
    game = new GameEngine();
    game.init();
    
    // 添加触摸事件支持（移动端）
    document.addEventListener('touchstart', function(e) {
        e.preventDefault();
    }, { passive: false });
    
    document.addEventListener('touchmove', function(e) {
        e.preventDefault();
    }, { passive: false });
    
    document.addEventListener('touchend', function(e) {
        e.preventDefault();
    }, { passive: false });
    
    // 防止页面缩放
    document.addEventListener('gesturestart', function(e) {
        e.preventDefault();
    });
    
    document.addEventListener('gesturechange', function(e) {
        e.preventDefault();
    });
    
    document.addEventListener('gestureend', function(e) {
        e.preventDefault();
    });
    
    // 添加键盘支持（桌面端）
    document.addEventListener('keydown', function(e) {
        if (game.gameState === 'battle') {
            switch(e.key) {
                case 'Enter':
                    game.endTurn();
                    break;
                case 'Escape':
                    // 显示菜单
                    break;
            }
        }
    });
    
    // 添加窗口大小变化监听
    window.addEventListener('resize', function() {
        // 调整游戏界面大小
        adjustGameSize();
    });
    
    // 初始化时调整大小
    adjustGameSize();
});

// 调整游戏界面大小
function adjustGameSize() {
    const gameContainer = document.getElementById('game-container');
    const screenWidth = window.innerWidth;
    const screenHeight = window.innerHeight;
    
    // 设置游戏容器大小
    gameContainer.style.width = screenWidth + 'px';
    gameContainer.style.height = screenHeight + 'px';
    
    // 调整卡牌大小
    const cards = document.querySelectorAll('.card');
    cards.forEach(card => {
        if (screenWidth < 768) {
            card.style.minWidth = '100px';
            card.style.fontSize = '0.8rem';
        } else {
            card.style.minWidth = '120px';
            card.style.fontSize = '1rem';
        }
    });
    
    // 调整角色卡片大小
    const characterCards = document.querySelectorAll('.character-card');
    characterCards.forEach(card => {
        if (screenWidth < 768) {
            card.style.width = '90%';
            card.style.maxWidth = '300px';
        } else {
            card.style.width = '200px';
            card.style.maxWidth = 'none';
        }
    });
}

// 工具函数
function showMessage(message, type = 'info') {
    // 创建消息元素
    const messageElement = document.createElement('div');
    messageElement.className = `message message-${type}`;
    messageElement.textContent = message;
    messageElement.style.cssText = `
        position: fixed;
        top: 20px;
        left: 50%;
        transform: translateX(-50%);
        background: rgba(0, 0, 0, 0.8);
        color: white;
        padding: 10px 20px;
        border-radius: 5px;
        z-index: 1000;
        font-size: 1rem;
    `;
    
    document.body.appendChild(messageElement);
    
    // 3秒后移除
    setTimeout(() => {
        messageElement.remove();
    }, 3000);
}

// 动画效果
function animateCard(cardElement, animationType) {
    switch (animationType) {
        case 'play':
            cardElement.style.transform = 'translateY(-10px) scale(1.05)';
            cardElement.style.boxShadow = '0 10px 25px rgba(0,0,0,0.5)';
            break;
        case 'discard':
            cardElement.style.transform = 'translateX(100px) rotate(45deg)';
            cardElement.style.opacity = '0';
            break;
        case 'draw':
            cardElement.style.transform = 'translateY(-20px)';
            cardElement.style.opacity = '0';
            break;
    }
    
    setTimeout(() => {
        cardElement.style.transform = '';
        cardElement.style.opacity = '';
        cardElement.style.boxShadow = '';
    }, 300);
}

// 音效管理（如果需要）
class SoundManager {
    constructor() {
        this.sounds = {};
        this.enabled = true;
    }
    
    play(soundName) {
        if (!this.enabled) return;
        
        // 这里可以添加音效播放逻辑
        console.log(`播放音效: ${soundName}`);
    }
    
    setEnabled(enabled) {
        this.enabled = enabled;
    }
}

// 全局音效管理器
const soundManager = new SoundManager();
